package com.zsjt.app.compoent.utils.phone;

import com.zsjt.app.compoent.utils.phone.Config;
import com.zsjt.app.compoent.utils.phone.HttpUtil;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
@Component("phoneCode")
public class PhoneCode {

    // 根据相应的手机号发送验证码
    public boolean sendPhoneCode(String phone,String code) throws Exception {
        StringBuilder sb = new StringBuilder();
        sb.append("accountSid").append("=").append(Config.ACCOUNT_SID);
        sb.append("&to").append("=").append(phone);
        //sb.append("&param").append("=").append(URLEncoder.encode("","UTF-8"));
        //sb.append("&templateid").append("=").append("1251");
        sb.append("&smsContent").append("=").append(URLEncoder.encode("【北京沃尔沃公司】您的短信验证码为"+code+"，2分钟内有效，若非本人操作请忽略。","UTF-8"));
        String body = sb.toString() + HttpUtil.createCommonParam(Config.ACCOUNT_SID, Config.AUTH_TOKEN);
        String result = HttpUtil.post(Config.BASE_URL, body);
        if(result.indexOf("成功")!=-1) {
            return true;
        }else {
            return false;
        }
    }

    //创建手机短信验证码
    public String getPhoneCode() {
        String random = (int) ((Math.random() * 9 + 1) * 100000) + "";
        return random;
    }

//    public static void main(String[] args) {
//        try {
//            boolean b = sendPhoneCode("19173027219", getPhoneCode());
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
}
